package com.example.mymvvmdemo.models.main.fragment.device.adapter;

import android.content.Context;
import android.view.View;


import androidx.recyclerview.widget.GridLayoutManager;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseVBAdapter;
import com.example.mymvvmdemo.constants.Constants;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.ItemDeviceBinding;
import com.example.mymvvmdemo.models.main.fragment.device.entity.DeviceListEntity;
import com.example.mymvvmdemo.models.main.fragment.device.entity.DeviceShortEntity;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.StrUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;

public class DeviceAdapter extends BaseVBAdapter<DeviceListEntity.DataDTO.DevicesDTO, ItemDeviceBinding> {

    public Gson gson = new Gson();
    public DeviceQuickListener listener;
    public final int TYPE_DEVICE = 0;
    public final int TYPE_ADD = 1;

    public void setListener(DeviceQuickListener listener) {
        this.listener = listener;
    }

    /**
     * @param context
     */
    public DeviceAdapter(Context context, ArrayList<DeviceListEntity.DataDTO.DevicesDTO> list) {
        super(context);
        setDataList(list);
    }

    @Override
    protected void updateData(ItemDeviceBinding bind, DeviceListEntity.DataDTO.DevicesDTO item, int position) {

        if (item == null) {
            initAddView(bind, item, position);
        } else {
            initDeviceView(bind, item, position);
        }

    }

    private void initDeviceView(ItemDeviceBinding bind, DeviceListEntity.DataDTO.DevicesDTO item, int position) {
        bind.clAdd.setVisibility(View.GONE);
        bind.clDevice.setVisibility(View.VISIBLE);

        String name = item.getName();
        if (!StrUtils.isEmpty(name)) {
            bind.tvTitle.setText(name);
        }

        String status = item.getStatus();
        boolean isOffline = status.equals(Constants.OFFLINE);


        bind.tvState.setTextColor(isOffline ? context.getColor(R.color.gray_two) : context.getColor(R.color.green_one));
        bind.tvState.setText(isOffline ? context.getString(R.string.offline) : context.getString(R.string.online));
        bind.tvState.setBackground(isOffline
                ? context.getResources().getDrawable(R.drawable.bg_twenty_three_20_round)
                : context.getResources().getDrawable(R.drawable.bg_green_seven_20_round));


        startLoadDeviceFunction(item, bind);

        bind.clDevice.setOnClickListener(view -> {
            if (listener != null) {
                listener.onAddDeviceClick(position, TYPE_DEVICE);
            }
        });

    }

    private void initAddView(ItemDeviceBinding bind, DeviceListEntity.DataDTO.DevicesDTO item, int position) {
        bind.clAdd.setVisibility(View.VISIBLE);
        bind.clDevice.setVisibility(View.GONE);

        bind.clAdd.setOnClickListener(view -> {
            if (listener != null) {
                listener.onAddDeviceClick(position, TYPE_ADD);
            }
        });

    }

    public void startLoadDeviceFunction(DeviceListEntity.DataDTO.DevicesDTO item, ItemDeviceBinding bind) {
        GetPresenter getPresenter = new GetPresenter(new IView.GetView() {
            @Override
            public void loadGetSuccess(String result, String type, int code) {
                DeviceShortEntity deviceShortEntity = gson.fromJson(result, DeviceShortEntity.class);
                ArrayList<DeviceShortEntity.DataDTO.ShortcutsDTO> shortcuts = deviceShortEntity.getData().getShortcuts();


                DeviceQuickAdapter adapter = new DeviceQuickAdapter(context, shortcuts);
                bind.recyclerView.setAdapter(adapter);
                bind.recyclerView.setLayoutManager(new GridLayoutManager(context, shortcuts.size()));
                adapter.setListener(new DeviceQuickAdapter.DeviceQuickListener() {
                    @Override
                    public void onQuickClick(int position) {
                        if (listener != null) {
                            DeviceShortEntity.DataDTO.ShortcutsDTO shortcutsDTO = adapter.getDataList().get(position);
                            listener.onQuickClick(position, item, shortcutsDTO);
                        }
                    }
                });

            }

            @Override
            public void loadGetFail(String message, String type) {

            }

            @Override
            public void loadGetAccountError(int code) {

            }

            @Override
            public void loadGetNetWorkError() {

            }
        });
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("device_id", item.getDevice_id());
        getPresenter.startLoadGet(UrlConstants.deviceShortcuts, hashMap, UrlTypeConstants.deviceShortcuts);
    }

    public interface DeviceQuickListener {
        void onQuickClick(int position, DeviceListEntity.DataDTO.DevicesDTO item, DeviceShortEntity.DataDTO.ShortcutsDTO dto);

        void onAddDeviceClick(int position, int type);
    }

}
